::version 20220422
::version 20240411 增加关于Lua脚本加密的调用
@echo off
setlocal ENABLEDELAYEDEXPANSION

rem 目录设置
pushd %cd%
chcp 65001
cd  %~dp0
set CURDIR=%cd%
if not defined BUILDTOOL (
    set BUILDTOOL=%CURDIR%\..\..\buildtool
)
rem tools
set SVNTOOL=%BUILDTOOL%\Git\bin\git.exe
set INNO_SETUP_TOOL=%BUILDTOOL%\InnoSetup6\ISCC.exe
set VERSION_FILE=%CURDIR%\..\output\Package\sys.json
set GITPATH=%BUILDTOOL%\Git
set SEDTOOL="%GITPATH%\usr\bin\sed.exe"
set GREPTOOL=%BUILDTOOL%\Git\usr\bin\grep.exe
set VERSION_STR=1.0.0
set ISS_FILE=%CURDIR%\ISS\P53DC.iss
set LUA_CONVERT=%BUILDTOOL%\luaencrypt\index.js

rem 存放目录设置
set SETUP_PACKAGE=%CURDIR%\..\output\Package
set SETUP_SCRIPT=%CURDIR%\..\output\Package\script
set SETUP_PDB=%CURDIR%\..\output\Pdb
set SETUP_OUT=%CURDIR%\..\output\Setup

rem 制品保存目录
set REMOTE_SETUP_OUT= \\fs\swap\product\P53DC_NEW\setup\
set REMOTE_PDB_OUT=\\fs\swap\product\P53DC_NEW\pdb\
rem *****************************************************************
set BRANCHNAME=master
if not "%~1"=="" (  
  set BRANCHNAME=%~1
)

if defined DDTOKEN (
rem 说明在CI环境下
	rmdir /S /Q  %CURDIR%\..\cncbase\
	rmdir /S /Q  %CURDIR%\..\components\
	rmdir /S /Q  %CURDIR%\..\cncmachine\
)
rem 编译依赖项目
call AutoBuild.bat %1

if NOT %ERRORLEVEL% == 0 (
    echo "AutoBuild.bat 编译失败" %ERRORLEVEL%
    exit /b %ERRORLEVEL%
)


set CmdTitle=UPCNC Windows x64安装包客户端自动打包脚本
echo --------------------------------------------------------------
title %CmdTitle%
echo %CmdTitle%
echo.
echo.

echo 正在编译安装包...

call :SetSVNVersion
set setupname=UPCNC3_%VERSION_STR%
set CMTLOG=commitLog.txt
set OutputFilename=%setupname%
if not "%~1"=="" (  
  set OutputFilename=%OutputFilename%-%~1
  set CMTLOG=%~1-%CMTLOG%
)
call :enCryptLuaScript

call :PackageSetup

if defined CI_COMMIT_BRANCH (
::CI环境才复制到远程目录
	echo 正在备份制品到共享目录...
	call :CopyRemote

	echo 抽取git日志到共享目录...
	call :getGitlog
)
goto :END
rem *****************************************************************


:SetSVNVersion
%GREPTOOL% "pwd" -B 5 %VERSION_FILE% | %GREPTOOL% "ver" | %GREPTOOL% -Eo "[0-9.]+" > ver.tmp
for /f "tokens=1-4 delims= " %%1 in (ver.tmp) do set vertmp=%%1
set VERSION_STR=%vertmp%
rem if defined BUILDENV (
rem	echo VERSION_STR=%VERSION_STR% >>%BUILDENV%
rem )
del /Q ver.tmp
goto :eof

:PackageSetup
rem 生成安装包的部分
%INNO_SETUP_TOOL% /O%SETUP_OUT%\ /F%OutputFilename% /DMyAppVersion=%VERSION_STR% %ISS_FILE%
goto :eof

:CopyRemote
xcopy %SETUP_OUT%\%OutputFilename%.exe %REMOTE_SETUP_OUT% /E /F /R /Y
xcopy %SETUP_PDB%\* %REMOTE_PDB_OUT%\%VERSION_STR%\ /E /F /R /Y
rem if defined BUILDENV (
rem     echo REMOTEFILE=%REMOTE_SETUP_OUT%\%OutputFilename%.exe >> %BUILDENV%
rem )
goto :eof

:getGitlog
%SVNTOOL% log --pretty=format:"%%ai,%%h,%%an:%%s" -30 > %REMOTE_SETUP_OUT%%CMTLOG%
cd %CURDIR%\..\cncmachine
echo . >> %REMOTE_SETUP_OUT%%CMTLOG%
echo cncmachine >> %REMOTE_SETUP_OUT%%CMTLOG%
%SVNTOOL% log --pretty=format:"%%ai,%%h,%%an:%%s" -15 >> %REMOTE_SETUP_OUT%%CMTLOG%

cd %CURDIR%\..\components
echo . >> %REMOTE_SETUP_OUT%%CMTLOG%
echo components >> %REMOTE_SETUP_OUT%%CMTLOG%
%SVNTOOL% log --pretty=format:"%%ai,%%h,%%an:%%s" -15 >> %REMOTE_SETUP_OUT%%CMTLOG%

cd %CURDIR%\..\cncbase
echo . >> %REMOTE_SETUP_OUT%%CMTLOG%
echo cncbase >> %REMOTE_SETUP_OUT%%CMTLOG%
%SVNTOOL% log --pretty=format:"%%ai,%%h,%%an:%%s" -15 >> %REMOTE_SETUP_OUT%%CMTLOG%
cd %CURDIR%
rem if defined BUILDENV (
rem 	echo REMOTEGITLOG=%REMOTE_SETUP_OUT%%CMTLOG% >>%BUILDENV%
rem )
goto :eof

:ENDERROR
echo %CmdTitle%错误，请检查log
pause
goto :eof

:enCryptLuaScript
for /d %%D in (%SETUP_SCRIPT%\*) do (
    for %%F in ("%%~dpnD"\*.lua) do (
        node %LUA_CONVERT% --in="%%F" --out="%%F" --p=up3d
    )
)
goto :eof


:END
popd